package com.huaxiafinance.management.controller;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxiafinance.management.model.AppVersion;
import com.huaxiafinance.management.service.AppVersionService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller("appVersionController")
@RequestMapping("/appVersion")
public class AppVersionController {

    private static final Logger log = Logger.getLogger(AppVersionController.class);

    @Autowired
    private AppVersionService appVersionSerivce;

    @RequestMapping(value = "queryVersion", produces = "application/json;charset=utf-8")
    public @ResponseBody
    String queryVersion() throws Exception {
        MessageResult messageResult = new MessageResult();

        List<AppVersion> versions = appVersionSerivce.select();
        messageResult.setData(versions);
        messageResult.setResultMessage(ServiceCode.SUCCESS);

        return messageResult.getAsJSON();
    }

    @RequestMapping(value = "addVersion", produces = "application/json;charset=utf-8")
    public @ResponseBody
    String addVersion(HttpServletRequest request) throws Exception {
        String json = request.getParameter("json");
        log.info("新增版本控制请求参数" + json);

        MessageResult messageResult = new MessageResult();

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        AppVersion appVersion = mapper.readValue(json, AppVersion.class);

        boolean flag = appVersionSerivce.insert(appVersion);

        if (flag)
            messageResult.setResultMessage(ServiceCode.SUCCESS);
        else
            messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);

        log.info("新增版本控制返回参数" + messageResult.getAsJSON());
        return messageResult.getAsJSON();
    }

    @RequestMapping(value = "updateVersion", produces = "application/json;charset=utf-8")
    public @ResponseBody
    String updateVersion(HttpServletRequest request) throws Exception {
        String json = request.getParameter("json");
        log.info("修改版本控制请求参数" + json);

        MessageResult messageResult = new MessageResult();

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        AppVersion appVersion = mapper.readValue(json, AppVersion.class);

        boolean flag = appVersionSerivce.update(appVersion);

        if (flag)
            messageResult.setResultMessage(ServiceCode.SUCCESS);
        else
            messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);

        log.info("修改版本控制返回参数" + messageResult.getAsJSON());
        return messageResult.getAsJSON();
    }

    @RequestMapping(value = "delVersion", produces = "application/json;charset=utf-8")
    public @ResponseBody
    String delVersion(Integer id) throws Exception {
        log.info("删除版本控制请求参数" + id);

        MessageResult messageResult = new MessageResult();

        boolean flag = appVersionSerivce.del(id);

        if (flag)
            messageResult.setResultMessage(ServiceCode.SUCCESS);
        else
            messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);

        log.info("删除版本控制返回参数" + messageResult.getAsJSON());
        return messageResult.getAsJSON();
    }
}
